﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SS2TC.Lexicon;

namespace SS2TC.Speech
{
    /// <summary>
    /// Recognition engine.
    /// </summary>
    public class Recognizer
    {
        private Index lexicon;

        /// <summary>
        /// Initializes a new instance of the recognizer.
        /// </summary>
        /// <param name="lexicon">Index to perform to recognition.</param>
        public Recognizer(Index lexicon)
        {
            this.lexicon = lexicon;
        }

        public bool Match(byte[] stream)
        {
            foreach (byte[] data in lexicon.Values)
            {
                bool match = true;

                for (int i = 0; (i < stream.Length) && (i < data.Length); i++)
                {
                    if ((stream[i] < data[i] - 5) || (stream[i] > data[i] + 5))
                    {
                        match = false;
                    }
                }

                if (match)
                {
                    return true;
                }
            }

            return false;
        }

        public bool Match(byte[] stream1, byte[] stream2)
        {
            byte[] data1 = new byte[stream1.Length];
            byte[] data2 = new byte[stream2.Length];

            int j = 0;
            for (int i = 0; i < stream1.Length; i++)
            {
                if (stream1[i] != 128)
                {
                    data1[j] = stream1[i];
                    j++;
                }
            }

            j = 0;
            for (int i = 0; i < stream2.Length; i++)
            {
                if (stream2[i] != 128)
                {
                    data2[j] = stream2[i];
                    j++;
                }
            }

            return false;
        }
    }
}
